#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_RETRIES 3
#define PACKET_SIZE 1024

int main() {
    FILE *inputFile, *outputFile;
    char buffer[PACKET_SIZE];
    int retries = 0;

    // 打开输入文件
    inputFile = fopen("input.txt", "rb");
    if (inputFile == NULL) {
        fprintf(stderr, "无法打开输入文件\n");
        return 1;
    }

    // 创建或覆盖输出文件
    outputFile = fopen("output.txt", "wb");
    if (outputFile == NULL) {
        fprintf(stderr, "无法创建输出文件\n");
        fclose(inputFile);
        return 1;
    }

    // 读取数据并写入输出文件
    while (!feof(inputFile)) {
        size_t bytesRead = fread(buffer, 1, PACKET_SIZE, inputFile);
        if (bytesRead > 0) {
            size_t bytesWritten = fwrite(buffer, 1, bytesRead, outputFile);
            if (bytesWritten < bytesRead) {
                // 写入不完整，进行重传
                if (retries < MAX_RETRIES) {
                    fseek(inputFile, -bytesRead, SEEK_CUR);  // 回退文件指针
                    retries++;
                } else {
                    fprintf(stderr, "重传次数过多，放弃重传\n");
                    break;
                }
            }
        }
    }

    // 关闭文件
    fclose(inputFile);
    fclose(outputFile);

    return 0;
}